//
//  DetailsViewController.m
//  UI_元旦豆瓣半完整版
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DetailsViewController.h"
#import "UIImageView+WebCache.h"
#import "MyDataBaseHandle.h"
#import "MovieModel.h"

#define WIDCH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface DetailsViewController ()

@end

@implementation DetailsViewController

- (void)dealloc {
    [_detail release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    UIImage *shouImage = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    shouImage = [shouImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:shouImage style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    UIImage *backImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(item2Action:)];
    self.navigationItem.leftBarButtonItem = item2;
    
    
    
//    NSLog(@"%@", self.detail[@"movieName"]);
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDCH, HEIGHT)];
    [self.view addSubview:scroll];
    scroll.contentSize = CGSizeMake(0, HEIGHT);
    [scroll release];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, WIDCH - 40, 50)];
    lable.text = _detail[@"movieName"];
    lable.font = [UIFont systemFontOfSize:25];
    lable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lable];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 100, 150)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_detail[@"pic_url"]]];
    [self.view addSubview:imageView];
    
    
}

- (void)item2Action:(UIBarButtonItem *)item {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)itemAction:(UIBarButtonItem *)item {
    NSArray *arr = [[MyDataBaseHandle sharedDataBaseHandle] selectTableMovie];
    if (![arr containsObject:_detail[@"movieName"]]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"收藏成功😎" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        
        // 收藏到数据库
        MovieModel *model = [MovieModel movieModel];
        model.original_title = _detail[@"movieName"];
        [[MyDataBaseHandle sharedDataBaseHandle] insertMovieModel:model];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已收藏过了！👻" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
